#include "PanelTreeItem.h"
#include "GraphicsButton.h"
#include "NodeManager.h"
#include "EntryItem.h"
#include "vmLibrary.h"
#include "xMappingNode.h"
#include <QtGui/QFont>
#include <QtGui/QFontMetrics>
#include <QtGui/QPainter>
#include <cassert>

PanelTreeItem::PanelTreeItem(std::string name, AbstractManager* manager, AbstractPanel* panel)
	: PanelItem(name, manager, panel)
{
	this->_unfoldButton = NULL;
	this->_level = 1;
	this->_width = 254;
}

PanelTreeItem::~PanelTreeItem()
{

}

int PanelTreeItem::getlevel()
{
	return _level;
}


GraphicsButton* PanelTreeItem::getIcon()
{
	return this->_unfoldButton;
}

int PanelTreeItem::indexOfChild( EntryItem* child )
{
	int index = 0;

	for ( int i = 0; i < _children.size(); ++i )
	{
		if ( _children.at(i) == child){
			index = i;
			break;
		}
	}

	return index;
}


void PanelTreeItem::coumputelevel()
{
	int levelCount = 1;

	PanelItem* item = this;
	while(item->getParent()!=NULL){
		levelCount++;
		item = item->getParent();
	}

	_level = levelCount;
}

void PanelTreeItem::setParent( PanelItem* parent)
{
	PanelItem::setParent(parent);
	coumputelevel();
	if ( _unfoldButton!= NULL ){
		_unfoldButton->setPos(LEFT_PADDING*getlevel()-15, _unfoldButton->y());
	}

}

void PanelTreeItem::setIsUnfold( bool isUnfold )
{
	assert(_unfoldButton!=NULL);
	this->_isUnfold = isUnfold;
	_mpnNode->setUnfold(_isUnfold);

	if ( isUnfold == false ){
		QPixmap newIcon(PLUSICON);
		_unfoldButton->setIcon(newIcon);
	} else {
		QPixmap newIcon(MINUSICON);
		_unfoldButton->setIcon(newIcon);
	}

}

void PanelTreeItem::clickUnfoldButton()
{
	if ( _isUnfold == false){
		setIsUnfold(true);
		unfold(true);
	} else {
		setIsUnfold(false);
		unfold(false);
	}
}

void PanelTreeItem::setChildrenVisible(bool isVisible)
{
	std::vector<EntryItem*>::iterator itr;
	for ( itr = _children.begin(); itr != _children.end(); ++itr )
	{
		(*itr)->setVisible(isVisible);
		(*itr)->setChildrenVisible((isVisible && (*itr)->isUnfold()));
	}
}



bool PanelTreeItem::isUnfold()
{
	return this->_isUnfold;
}


void PanelTreeItem::drawType( QPainter * painter )
{
	QFont sansFont("Helvetica", 8);
	QFontMetrics fontMetrics(sansFont);
	int typeBlockWidth = fontMetrics.width(_type.c_str()) + TYPE_BLOCK_LEFT_PADDING + TYPE_BLOCK_RIGHT_PADDING;
	int typeBlockHeight = fontMetrics.height();

	painter->setBrush(QBrush(QColor("#575757")));
	painter->drawRoundedRect( LEFT_PADDING*getlevel() , TOP_PADDING, typeBlockWidth, typeBlockHeight, 2 ,2);

	painter->setPen(QPen(QColor("#FFFFFF")));
	painter->setFont(sansFont);
	painter->drawText( LEFT_PADDING*getlevel(), TOP_PADDING, typeBlockWidth, typeBlockHeight, Qt::AlignCenter, trf(_type).c_str());
}

void PanelTreeItem::initUnfoldButton()
{
	QPixmap icon(PLUSICON);
	_unfoldButton = new GraphicsButton(icon, icon, this);
	_unfoldButton->setPos( 1 , HEIGHT/2 - icon.height()/2 );
	_unfoldButton->setParent(this);
	connect( _unfoldButton, SIGNAL(triggered()), this, SLOT(clickUnfoldButton()));
}

void PanelTreeItem::hideUnfoldIcon() {
	this->unfold( false );
	this->_unfoldButton->hide();
}
